package com.hyt.it.ogt.ykcj.service.grade.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.ykcj.domain.grade.EnterpriseExamineeCertificateRelation;
import com.hyt.it.ogt.ykcj.domain.zs.ZsInfo;
import com.hyt.it.ogt.ykcj.mapper.grade.EnterpriseExamineeCertificateRelationMapper;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseExamineeCertificateRelationService;
import com.hyt.it.ogt.ykcj.service.zs.IZsInfoService;

import cn.hutool.core.util.ArrayUtil;

/**
 * <p>
 * 企业考生证书关联 服务实现类
 * </p>
 *
 * @author yanghuankun@talkweb.com.cn
 * @since 2023-06-12
 */
@Service
public class EnterpriseExamineeCertificateRelationServiceImpl extends BaseServiceImpl<EnterpriseExamineeCertificateRelationMapper, EnterpriseExamineeCertificateRelation> implements IEnterpriseExamineeCertificateRelationService {

    @Resource
    IZsInfoService zsInfoService;

    @Override
    public List<EnterpriseExamineeCertificateRelation> listByExamineeInfoId(String examineeInfoId) {
        return this.lambdaQuery()
                .eq(EnterpriseExamineeCertificateRelation::getEnterpriseExamineeInfoId, examineeInfoId)
                .list();
    }

    @Override
    public List<String> listNameByExamineeInfoId(String examineeInfoId) {
        List<EnterpriseExamineeCertificateRelation> enterpriseExamineeCertificateRelations = listByExamineeInfoId(examineeInfoId);
        return enterpriseExamineeCertificateRelations.stream()
                .map(EnterpriseExamineeCertificateRelation::getZsInfoId)
                .map(zsInfoService::getById)
                .map(ZsInfo::getCertificateName)
                .collect(Collectors.toList());
    }

    @Override
    public List<String> listCanViewByExaminee(String examId, String examineeId, List<String> zsInfoIds) {
        return this.lambdaQuery()
                .eq(EnterpriseExamineeCertificateRelation::getEnterpriseExamineeInfoId, examineeId)
                .eq(EnterpriseExamineeCertificateRelation::getExamInfoId, examId)
                .in(EnterpriseExamineeCertificateRelation::getZsInfoId, zsInfoIds)
                .list()
                .stream()
                .map(EnterpriseExamineeCertificateRelation::getZsExamineeInfoId)
                .distinct()
                .collect(Collectors.toList());
    }

    @Override
    public void removeByExamineeInfoIds(String... examineeInfoIs) {
        if (ArrayUtil.isEmpty(examineeInfoIs)) {
            return;
        }
        this.lambdaUpdate()
                .in(EnterpriseExamineeCertificateRelation::getEnterpriseExamineeInfoId, examineeInfoIs)
                .remove();
    }
}
